var IMG_WIDTH=900;
var IMG_HEIGHT=600;

var main_slideshow='camera_wrap_1';
var main_audio_control='audio_controller';
var div_audio_container='div_audio_container';

var slides;
var slide_idx;
var is_audio_supported=true;

function startup(){
	init_params();
	contact_session(TYPE_RETRIEVE_SLIDES,null);
}

function init_params(){
	slides={};
	slide_idx=0;
}

function add_audio(id,data){
	/** check if browser supports audio tag */
	var myAudio = document.createElement('audio'); 
	is_audio_supported=myAudio.canPlayType;
	/*
	if (is_audio_supported){
	   var canPlayMp3 = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/mpeg');
	   var canPlayOgg = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/ogg; codecs="vorbis"');
	}
	*/
	
	var e=data.trim().split(splitter);
	$('#'+div_audio_container).
		append(''+
			'<audio id="'+main_audio_control+'" src="'+e[2]+'" '+ 
			'		autoplay="autoplay" '+ 
			'		preload="auto" type="audio/mpeg" autobuffer>'+
			'</audio>');
	
	audiojs.events.ready(function() {
	    audiojs.createAll();
	});
}

function add_slide(id,data){
	var e=$(data).find('input').val().
					trim().split(splitter);
	var type=e[0];
	
	var slideContent='';
	if (type==TYPE_IMAGE_SLIDE){
		slideContent=get_image_slide(id,data);
	}else if (type==TYPE_U2B_SLIDE){
		slideContent=get_u2b_slide(id,data);
	}else if (type==TYPE_ARTICLE_SLIDE){
		slideContent=get_article_slide(id, data);
	}else if (type==TYPE_FB_SLIDE){
		
	}
	slides[id]=slideContent;
	$('#'+main_slideshow).append(''+slideContent);
}

function get_article_slide(id,data){
	var e=$(data).find('input').val().trim().split(splitter);
	$.each(e,function(k,v){
		e[k]=e[k].replace(prime,'\'');
		e[k]=e[k].replace(dprime,'\"');
	});
	var thumb=e[1];
	var img=e[2];
	var info=e[3].trim().
		split(data_splitter)[3].split(inline_splitter);

	return ''+
		'<div data-thumb="'+thumb+'" data-src="'+img+'">'+
		'<div class="camera_caption fadeFromBottom">'+
			'<b>'+info[0]+'</b> <br><br>'+
			'<div align="left">'+info[2]+'<br><br>'+
			'<a onclick=\'window.open("'+info[3]+'","news");\'>next >></a>'+
			'</div>'+
		'</div>';
}

function get_u2b_slide(id,data){
	var e=$(data).find('input').val().trim().split(splitter);
	var thumb=e[1];
	var img=e[2];
	var info=e[3].trim().
		split(data_splitter)[3].split(inline_splitter);
	
	return ''+
		'<div data-thumb="'+thumb+'" data-src="'+img+'">'+
		'<div class="camera_caption fadeFromBottom">'+
			'<b>'+info[0]+'</b> (by '+info[3]+')<br><br>'+
			'<div>'+embed_video(info[4])+'</div><br>'+
			'<div align="left">'+info[1]+'<br><br>'+
			'<b>View: '+info[2]+'</b></div>'+
		'</div>';
}

function embed_video(url){
	return ''+
	'<object width="300" height="200">'+ 
		'<param name="movie" value="'+url+'"></param>'+
		'<param name="allowFullScreen" value="true"></param>'+
		'<embed src="'+url+'" type="application/x-shockwave-flash" '+ 
			'allowfullscreen="true" width="300" height="200">'+
		'</embed>'+
	'</object>';
	'';
}

function get_image_slide(id,data){
	var e=$(data).find('input').val().trim().split(splitter);
	var thumb=e[1];
	var img=e[2];
	var info=e[3].split(data_splitter);
	return ''+
		'<div data-thumb="'+thumb+'" data-src="'+img+'">'+
	(info != ''?
		'<div class="camera_caption fadeFromBottom">'+info[3]+'</div>':'')+
		'</div>';
}

function contact_session(type,list){
	var ctype='retrieve-slides';
	if (type==TYPE_RETRIEVE){
		ctype='retrieve';
	}else if (type==TYPE_RETRIEVE_SLIDES){
		ctype='retrieve-slides';
	}
	
	slide_idx=0;
	
	$.ajax({
		url: './jsp/modules/session.jsp',
		data: {
			'type':ctype,
			'list':list,
			'id':0,
			'content':''
		},
		   
		type: 'post',
		success:function(data){
			var arr=jQuery.parseJSON(data);
			var e=null,k=null,v=null;
			$.each(arr,function(){
				e=this.split(server_splitter);
				k=e[0];
				v=e[1];
				if (k.indexOf('audio') >=0){
					add_audio(k,v);
				}else{
					add_slide(k,v);
					slide_idx++;
				}
				
			});
			/*
			$.each(arr,function(k, v){
				add_slide(k,v);
				slide_idx++;
			});
			*/
			startup_slideshow();
		},		
	});
}

function startup_slideshow(){
	jQuery('#camera_wrap_1').camera({
		time: 5000,
		height: '50%',
		thumbnails: true,
		onPlay:function(){
			if (is_audio_supported){
				if (!(typeof($('#'+main_audio_control)[0])
						===undefined_obj))
					$('#'+main_audio_control)[0].play();
			}
		},
		onStop:function(){
			if (is_audio_supported){
				if (!(typeof($('#'+main_audio_control)[0])
						===undefined_obj))
					$('#'+main_audio_control)[0].pause();
			}
		}
	});
}